home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / trojka.tgz / trojka.tar / trojka / system.c < prev    next >
C/C++ Source or Header  |  1995-10-20  |  2KB  |  139 lines

  1. /*
  2.  *    @(#) system.c -- lowlevel system functions for TROJKA
  3.  *    created:    4.iii.1992 for XENIX/68000
  4.  *    modified:     16.iv.1992 Maarten added SunOS + HPUX support
  5.  *            20.x.1995  Maarten added Linux support
  6.  */
  7.  
  8. #ifdef HPUX
  9. #include <codelibs/nap.h>
  10. #endif
  11.  
  12. #ifdef LINUX
  13. #include <termios.h>
  14. #endif
  15.  
  16. #ifdef XENIX68
  17. #include <sys/types.h>
  18. #include <sys/timeb.h>
  19. #endif
  20. #include <signal.h>
  21. #include <curses.h>
  22.  
  23. #include <stdio.h>
  24. #include <time.h>
  25. #include <fcntl.h>
  26.  
  27. #include "trojka.h"
  28.  
  29. extern flag curses_installed;
  30.  
  31. int     killkeys(),
  32.     quit_prog();
  33.  
  34. char    getkey();
  35.  
  36. int initcurses();
  37. int catch();
  38.  
  39. #if XENIX68
  40. struct timeb *tb;        /* for delay */
  41. #endif
  42.  
  43. char getkey()            /* get key from keyboard    */
  44. {
  45. #if SUNOS | HPUX | LINUX
  46.     long count;
  47.     char ch;
  48.  
  49.     ioctl(0,FIONREAD,&count);    /* check if there's something to read */
  50.     if(count > 0)
  51.         return(ch = getchar());
  52.     return(0);
  53. #endif
  54. #if XENIX68
  55.     static char buf[1];
  56.  
  57.     if(rdchk(0)>0) {    
  58.         read(0,buf,1);
  59.         return(buf[0]);
  60.     }
  61. #endif
  62. }
  63.  
  64.  
  65.  
  66. int killkeys()                /* flush keyboard buffer    */
  67. {
  68.     fflush(stdin);        
  69. }
  70.  
  71.  
  72. int initcurses()
  73. {
  74. #ifdef SUNOS
  75.     struct sgttyb terminfo;
  76.  
  77.     ioctl(0,TIOCGETP,&terminfo);
  78. #endif
  79.     initscr();
  80.     crmode();
  81.     nonl();
  82.     noecho();
  83.     signal(SIGTERM, catch);
  84.     signal(SIGINT, catch);
  85.     curses_installed = TRUE;
  86.  
  87. #ifdef SUNOS
  88.     if (LINES < 24 || COLS < 80) 
  89.         quit_prog("Sorry. The screensize must be at least 80 x 24");
  90. #endif
  91. }
  92.  
  93.  
  94. int catch(sig)
  95. int sig;
  96. {
  97.     signal(SIGTERM, SIG_IGN);
  98.     signal(SIGINT,  SIG_IGN);
  99.     
  100.     quit_prog("See you around in the Trojka-zone...");
  101. }
  102.  
  103. int quit_prog(s)
  104. char *s;
  105. {
  106.     if(curses_installed) {
  107.         clear();
  108.         refresh();
  109.         echo();
  110.         nl();
  111.         nocrmode();
  112.         endwin();
  113.     }
  114.     fprintf(stderr,"%s\n",s);
  115.     exit(0);
  116. }
  117.  
  118.  
  119. int delay(millisecs)
  120. int millisecs;
  121. {
  122. #if SUNOS | LINUX
  123.     usleep(millisecs * 1200);    /* usleep does microsecs; need millisecs */
  124. #endif
  125. #ifdef HPUX
  126.     nap(millisecs);
  127. #endif
  128. #if XENIX68
  129. /* believe me, this works */
  130.     long goal;
  131.  
  132.     ftime(tb);
  133.      goal = (long)(tb->time*(long)1000)+(tb->millitm/40)+(long)(millisec/50);
  134.          while((long)(tb->time*(long)1000)+(tb->millitm/40)<goal)
  135.             ftime(tb);
  136. #endif
  137. }
  138.  
  139.